package com.ruoyi.community.controller.wxApi;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.community.domain.User;
import com.ruoyi.community.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 用户Controller
 *
 * @author wj
 * @date 2022-02-22
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/wx/community/user")
@Api(tags = {"用户"})
public class WxUserController extends BaseController
{
    @Autowired
    private IUserService userService;

    /**
     * 查询用户列表
     */
    @ApiOperation(value = "查询用户列表")
    @GetMapping("/list")
    public TableDataInfo list(User user)
    {
        startPage();
        List<User> list = userService.selectUserList(user);
        if (ObjectUtils.isEmpty(list)){
            return getNoDataTable(list);
        }
        return getDataTable(list);
    }

    /**
     * 导出用户列表
     */
    @ApiOperation(value = "导出用户列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, User user)
    {
        List<User> list = userService.selectUserList(user);
        ExcelUtil<User> util = new ExcelUtil<User>(User.class);
        util.exportExcel(response, list, "用户数据");
    }

    /**
     * 获取用户详细信息
     */
    @ApiOperation(value = "获取用户详细信息")
    @GetMapping(value = "/getInfo")
    public AjaxResult getInfo( Long id)
    {
        return AjaxResult.success(userService.selectUserById(id));
    }

    /**
     * 新增用户
     */
    @ApiOperation(value = "新增用户")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody User user)
    {
        //return toAjax(userService.insertUser(user));
        user.setPassword(user.getWeixinOpenid());
        user.setStatus(0);
        int i = userService.insertUser(user);

        if (i<1){
            return AjaxResult.error();
        }
        return AjaxResult.success(user);
    }

    /**
     * 修改用户
     */
    @ApiOperation(value = "修改用户")
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody User user)
    {
        return toAjax(userService.updateUser(user));
    }

    /**
     * 删除用户
     */
    @ApiOperation(value = "删除用户")
    @DeleteMapping("/remove/{id}")
    public AjaxResult remove(@PathVariable("id") Long id) {
        Long[] ids= {id};
        return toAjax(userService.updateUserByIds(ids));
    }
}
